package com.yaozheng.vocationaltraining.service.impl.userinfo;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.iview.userinfo.IUpdateUserInfoView;
import com.yaozheng.vocationaltraining.service.userinfo.UpdateUserInfoService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

import java.util.HashMap;
import java.util.Map;

/**
 * 修改资料
 */
@EBean
public class UpdateUserInfoServiceImpl implements UpdateUserInfoService {
    IUpdateUserInfoView iUpdateUserInfoView;

    @Override
    public void init(IUpdateUserInfoView iUpdateUserInfoView) {
        this.iUpdateUserInfoView = iUpdateUserInfoView;
    }

    @Background
    @Override
    public void updateUserInfo(JSONArray currUserInfoJsonArray) {
        try {
            Map<String, String> para = new HashMap<String, String>();
            for (int i = 0; i < TypeUtils
                    .getJsonArraySize(currUserInfoJsonArray); i++) {
                JSONObject userInfoJsonObject = TypeUtils.getJsonObject(
                        currUserInfoJsonArray, i);
                if (2 == TypeUtils.getJsonInteger(userInfoJsonObject,
                        "isUpdate", 1)) {

                    String value = TypeUtils.getJsonString(
                            userInfoJsonObject, "value");
                    if (value != null && !"".equals(value) && !"null".equals(value)) {
                        para.put(TypeUtils.getJsonString(userInfoJsonObject,
                                "nameKey"), TypeUtils.getJsonString(
                                userInfoJsonObject, "value"));
                    }
                }
            }
            OkHttpClientManager.getInstance().post(
                    Constants.ROOTURL + "/user/info",
                    iUpdateUserInfoView.getToken(), para,
                    new OkHttpClientManager.IBaseViewCallback(new BaseView(iUpdateUserInfoView) {

                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iUpdateUserInfoView.updateUserInfoSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iUpdateUserInfoView.updateUserInfoError(error);
                        }


                    }));
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iUpdateUserInfoView.isResponseResult()) {
                iUpdateUserInfoView
                        .updateUserInfoError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

}
